package com.symbio.ishenapi.background.controller;

import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.background.model.*;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("tmOperationLogController")
public class TmOperationLogController extends BaseController {

    /**
     * @Author jianghy
     * @Description log日志内容增加
     * @parm operationContent 操作内容
     * operationType    操作类型（增加 、删除（逻辑删除）、修改 、查询）
     * operatorId       操作用户id
     * @return void
     */
   public void saveLogContent(){
       TmOperationLog tmOperationLog = (TmOperationLog)getReqBodyObject(TmOperationLog.class);
       TmOperationLog tol = new TmOperationLog();
       tol.setOperatorId(tmOperationLog.getOperatorId());
       tol.setOperationTime(new Date());
       tol.setOperationContent(tmOperationLog.getOperationContent());
       tol.setOperationType(tmOperationLog.getOperationType());
       if(tol.save()){
           renderJsonRet(50001);
       }else{
           renderJsonError(50002,"日志记录插入");
       }

    }
    /**
     * @Author jianghy
     * @Description 日志查询
     * @date 2018/12/3 14:54
     * @return void
     */
    public void queryLogContent(){
        //页面根据登录用户给传的user Id
        String userId = getPara("userId");
        //接收电话号码，值可以为空
        String phoneNumber = getPara("userMobilePhone");
        //根据roleId查询权限type
        BigInteger urId = BigInteger.valueOf(new Long(userId));
        List<TmRoleidUsertype> list = TmRoleidUsertype.dao.findIdToType(urId);
        if(phoneNumber==null||"".equals(phoneNumber)||phoneNumber=="null"){
            phoneNumber = "%";
        }
        int bs = 0;//权限标识
        for(int i=0;i<list.size();i++){
            TmRoleidUsertype tru = list.get(i);
            if(tru.getType()==1){
                bs = 1;
            }else if(tru.getType()==2){
                if(bs !=1){
                    bs = 2;
                }
            }else if(tru.getType()==3){
                if(bs !=1||bs !=2){
                    bs = 3;
                }
            }
        }
        if(bs!=0){
            Map map = TmOperationLog.dao.findList(bs,userId,getQueryModel(),phoneNumber);
            renderPageJsonRet(map.get("count"),map.get("data"));
        }else{
            renderJsonError(50002,"您没有被分配角色,查询");
        }
    }
}
